﻿#pragma warning disable 1591

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace TotalFreedomBST
{
    public enum VisiOptions
    {
        Toggle = 0,
        Show = 1,
        Hide = 2,
        Slow = 4,
        Fast = 8,
        SlideToggle = 16,
        FadeToggle = 32,
        HiddenByDefault = 64,
    }

    public class VisiToggler: IHtmlString
    {
        public string Id { get; set; }
        public string SectionId
        {
            get
            {
                return "vs_" + Id;
            }
        }

        public RouteValueDictionary HtmlAttributes { get; set; }
        public IHtmlString Content { get; set; }
        public VisiOptions Options { get; set; }
        internal bool HadOutput { get; set; }

        public VisiToggler(IHtmlString content, string id, VisiOptions options, RouteValueDictionary htmlAttributes)
        {
            Content = content;
            if (String.IsNullOrEmpty(id))
                Id = System.Guid.NewGuid().ToString("N");
            else
                Id = id;

            HtmlAttributes = htmlAttributes;
            Options = options;
        }

        public string ToHtmlString()
        {
            HadOutput = true;
            string result = "<div" + BstHelperExtensions.HtmlFromAttributes(HtmlAttributes);
            
            result += "><a href=\"#\" onclick=\"";

            if ((Options & VisiOptions.Show) == VisiOptions.Show)
                result += ShowJS();
            else if ((Options & VisiOptions.Hide) == VisiOptions.Hide)
                result += HideJS();
            else
                result += ToggleJS();

            result += " return false;\">" + Content.ToHtmlString() + "</a></div>";

            return result;
        }

        private string GetAnimationSpeed()
        {
            string speed = "100";
            if ((Options & VisiOptions.Slow) == VisiOptions.Slow)
                speed = "'slow'";
            else if ((Options & VisiOptions.Fast) == VisiOptions.Fast)
                speed = "'fast'";

            return speed;
        }

        public IHtmlString ShowJS(int revertInMilliseconds = -1)
        {
            string speed = GetAnimationSpeed();
            if (revertInMilliseconds > 0)
            {
                string firstSpeed = speed;
                if (!String.IsNullOrEmpty(speed))
                    firstSpeed = speed + ", ";

                return new MvcHtmlString("$('#" + HttpUtility.HtmlAttributeEncode(SectionId) + "').show(" + firstSpeed + " function() { $(this).delay(" + revertInMilliseconds + ").show(" + speed + "); } ); return false;");
            }
            else
                return new MvcHtmlString("$('#" + HttpUtility.HtmlAttributeEncode(SectionId) + "').show(" + speed + "); return false;");
        }

        public IHtmlString HideJS(int revertInMilliseconds = -1)
        {
            string speed = GetAnimationSpeed();
            if (revertInMilliseconds > 0)
            {
                string firstSpeed = speed;
                if (!String.IsNullOrEmpty(speed))
                    firstSpeed = speed + ", ";

                return new MvcHtmlString("$('#" + HttpUtility.HtmlAttributeEncode(SectionId) + "').hide(" + firstSpeed + " function() { $(this).delay(" + revertInMilliseconds + ").hide(" + speed + "); } ); return false;");
            }
            else
                return new MvcHtmlString("$('#" + HttpUtility.HtmlAttributeEncode(SectionId) + "').hide(" + speed + "); return false;");
        }

        public IHtmlString ToggleJS(int revertInMilliseconds = -1)
        {
            string toggle = "toggle";
            if ((Options & VisiOptions.SlideToggle) == VisiOptions.SlideToggle)
                toggle = "slideToggle";
            else if ((Options & VisiOptions.FadeToggle) == VisiOptions.FadeToggle)
                toggle = "fadeToggle";

            string speed = GetAnimationSpeed();
            if (revertInMilliseconds > 0)
            {
                string firstSpeed = speed;
                if (!String.IsNullOrEmpty(speed))
                    firstSpeed = speed + ", ";

                return new MvcHtmlString("$('#" + HttpUtility.HtmlAttributeEncode(SectionId) + "')." + toggle + "(" + firstSpeed + " function() { $(this).delay(" + revertInMilliseconds + ")." + toggle + "(" + speed + "); } ); return false;");
            }
            else
                return new MvcHtmlString("$('#" + HttpUtility.HtmlAttributeEncode(SectionId) + "')." + toggle + "(); return false;");
        }
    }

    public class VisiTogglerSection: BstSection
    {
        public VisiToggler VisiToggler { get; set; }
        public RouteValueDictionary HtmlAttributes { get; set; }

        public VisiTogglerSection(HtmlHelper htmlHelper, VisiToggler visiToggler, RouteValueDictionary htmlAttributes)
            : base(htmlHelper.ViewContext, "</div>")
        {
            HtmlAttributes = htmlAttributes;
            VisiToggler = visiToggler;
        }

        public override string StartHtml()
        {
            string s = "";
            if (!VisiToggler.HadOutput)
                s += VisiToggler.ToHtmlString();

            s += "<div id=\"" + HttpUtility.HtmlAttributeEncode(VisiToggler.SectionId) + "\"";

            if ((VisiToggler.Options & VisiOptions.HiddenByDefault) == VisiOptions.HiddenByDefault)
                s += " style=\"display: none;\"";


            if (HtmlAttributes != null)
                s += BstHelperExtensions.HtmlFromAttributes(HtmlAttributes);

            s += ">";

            return s;
        }
    }
}
